package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.pojo.RideLog;
import com.tbit.uqbike.client.pojo.TerPosition;
import com.tbit.uqbike.client.service.HistoryService;
import com.tbit.uqbike.client.service.MachineService;
import com.tbit.uqbike.client.service.RideLogService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 历史轨迹
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Controller
@RequestMapping("/history")
public class HistoryController {
    @Autowired
    private HistoryService historyService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private RideLogService rideLogService;

    /**
     * 查询轨迹
     *
     * @param mapType   地图类型
     * @param machineId 设备id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param session   会话
     * @param response  响应
     * @ignoreParams request
     */
    @RequestMapping("/query")
    public void query(Integer mapType, Integer machineId, String startTime, String endTime,
                      HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(machineId, "-30004", "machineId不能为空");
            Assert.hasLength(startTime, "-2004", "开始时间不能为空");
            Assert.hasLength(endTime, "-2005", "结束时间不能为空");

            if (mapType == null) {
                mapType = MapConstant.MAP_BAIDU;
            }

            /*查询设备信息*/
            Machine machine = machineService.getByMachineId(machineId);
            if (machine == null) {
                throw new BaseException("-30005", "设备不存在");
            }

            List<TerPosition> history = historyService.getByMachineNOAndTime(mapType, machine.getMachineNO(), startTime, endTime);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, history);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 通过订单编号查询订单轨迹
     *
     * @param mapType  地图类型
     * @param orderNO  订单编号
     * @param session
     * @param request
     * @param response
     */
    @RequestMapping("/getByOrderNO")
    public void getByOrderNO(Integer mapType, String orderNO, HttpSession session,
                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(orderNO, "-3064", "orderNO不能为空");

            if (mapType == null) {
                mapType = MapConstant.MAP_GOOGLE;
            }

            RideLog rideLog = rideLogService.getByOrderNO(orderNO);
            if (rideLog == null) {
                throw new BaseException("-3067", "不存在此订单");
            }
            List<TerPosition> history = historyService.getByMachineNOAndTime(mapType, rideLog.getMachineNO(), rideLog.getStartTime(), rideLog.getEndTime());

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, history);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

}